package com.anolesoft.amljmes.fortyone.controller;

//import com.alibaba.fastjson.JSONObject;

import com.anolesoft.amljmes.fortyone.dto.MesGjgyxxFkszInfoDto;
import com.anolesoft.amljmes.fortyone.service.MesGjgyxxFkszInfoService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/mesgjgyxxfksz")
public class MesGjgyxxFkszInfoController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String gyffjc;
        private String gxmgjzf;
        private Integer jzfk;
        private String factoryId;
    }

    @Autowired
    public MesGjgyxxFkszInfoService mesGjgyxxFkszInfoService;
    
    //查询
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesGjgyxxFkszInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());
        List<MesGjgyxxFkszInfoDto> allList = mesGjgyxxFkszInfoService.queryMesGjgyxxFkszInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesGjgyxxFkszInfoDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    } 
    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addMesGjgyxxFksz(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, mesGjgyxxFkszInfoService.addMesGjgyxxFksz(queryInfoDto));
    }
    //修改
    @PostMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesGjgyxxFkszInfoDto(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        System.out.println(">>>>>>update");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesGjgyxxFkszInfoService.updateMesGjgyxxFksz(queryInfoDto));
    }
    //删除
    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult deleteMesGjgyxxFksz(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        System.out.println(">>>>>>delete");
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesGjgyxxFkszInfoService.deleteMesGjgyxxFksz(queryInfoDto));
    }
    // 验证修改工艺方法简称名称重复
    @PostMapping(value="/MesGjgyxxFkszNameRules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isMesGjgyxxFkszName(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String gyffjc = "";
        int count = mesGjgyxxFkszInfoService.isMesGjgyxxFkszName(queryInfoDto);
        System.out.println("gyffjc=ccccccccccccccccc"+count);
        if(count > 0){
        	gyffjc = "该工艺方法简称名称已存在！";
        }
        String o;
        return new AjaxResult(HttpStatus.SUCCESS, gyffjc);
    }
    // 验证修改名称工序名关键字符重复
    @PostMapping(value="/MesGjgyxxFkszNameRules2", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isMesGjgyxxFkszName2(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String gxmgjzf = "";
        int count = mesGjgyxxFkszInfoService.isMesGjgyxxFkszName2(queryInfoDto);
        System.out.println("gxmgjzf=ffffffffffffffffff"+count);
        if(count > 0){
            gxmgjzf = "该工序名关键字符名称已存在！";
        }
        String o;
        return new AjaxResult(HttpStatus.SUCCESS, gxmgjzf);
    }
    
    // 验证新增工艺方法简称名称重复
    @PostMapping(value="/MesGjgyxxFkszNameXzRules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isMesGjgyxxFkszNameXz(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String gyffjc = "";
        int count = mesGjgyxxFkszInfoService.isMesGjgyxxFkszNameXz(queryInfoDto);
        System.out.println("gxmgjzf=ffffffffffffffffff"+count);
        if(count > 0){
        	gyffjc = "该工艺方法简称名称已存在！";
        }
        String o;
        return new AjaxResult(HttpStatus.SUCCESS, gyffjc);
    }

    // 验证新增名称工序名关键字符重复
    @PostMapping(value="/MesGjgyxxFkszNameXzRules2", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isMesGjgyxxFkszNameXz2(@RequestBody MesGjgyxxFkszInfoDto queryInfoDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String gxmgjzf = "";
        int count = mesGjgyxxFkszInfoService.isMesGjgyxxFkszNameXz2(queryInfoDto);
        System.out.println("gxmgjzf=ffffffffffffffffff"+count);
        if(count > 0){
            gxmgjzf = "该工序名关键字符名称已存在！";
        }
        String o;
        return new AjaxResult(HttpStatus.SUCCESS, gxmgjzf);
    }

    //动态下拉
    @PostMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesGjgyxxFkszAll(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String factoryId=loginUser.getFacCode();
        List<MesGjgyxxFkszInfoDto> list = mesGjgyxxFkszInfoService.MesGjgyxxFkszAll(factoryId);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }
}
